<?php
namespace Common\BLL;
/**
 * Created by PhpStorm.
 * User: 14775
 * Date: 2017/6/1
 * Time: 11:02
 */
use Common\Lib\RedisHandle;
use Common\Lib\SMSLib;
class SMSBLL
{
    /**
     * 验证码发送
     * @param  int $mobile    手机号
     * @return array('success','code')     返回值
     */
    public function sendValidateCode($mobile)
    {
        $code = getCode(4);
        $find = M('sms_template')->where(array('sms_code'=>10001))->find();error_log($code);
        $text = str_replace(' #VerificationCode# ',$code,$find['sms_content']);
//        $sms = new SMSLib();
//        $json_data = $sms->sendSMS($mobile,$text);
        $json_data = json_encode(array("code"=>0 ,"msg"=> "发送成功" ,"count"=>1 ,"fee"=> 0.05, "unit"=>"RMB" ,"mobile"=> "15978518127", "sid"=> '15486383492'));
        $data_code = json_decode($json_data)->code;
        if ($data_code == 0 ){
            RedisHandle::saveSMSVCode($mobile,$code);
            return array('success'=>true,'code'=>200);
        }else{
            return array('success'=>false,'code'=>8010);
        }
        
    }

    /**
     * 验证码验证
     * @param int $mobile   手机号
     * @param int $code     验证码
     * @return array('success','code')     返回值
     */
    public function verifyCode($mobile, $code)
    {
        if (RedisHandle::getSMSVCode($mobile)){
            if ($code == RedisHandle::getSMSVCode($mobile)){
                return array('success'=>true,'code'=>200);
            }else{
                return array('success'=>false,'code'=>8011);
            }
        }else{
            return array('success'=>false,'code'=>8011);
        }
    }

    /**
     * @param int $mobile     手机号
     * @param int $sms_code   手机模板
     * @param array $data 短信模板中需替换的参数KEY
     */
    public  function sendMessage($mobile, $sms_code, $data)
    {
        $find = M('sms_template')->where(array('sms_code'=>$sms_code))->find();
        $text = $find['sms_content'];
        foreach ($data as $key => &$val){
            $text = str_replace(' #'.$key.'# ',$val,$text);
        }
        $sms = new SMSLib();
        $json_data = $sms->sendSMS($mobile,$text);
//        $json_data = json_encode(array("code"=>0 ,"msg"=> "发送成功" ,"count"=>1 ,"fee"=> 0.05, "unit"=>"RMB" ,"mobile"=> "15978518127", "sid"=> '15486383492'));
        $data_code = json_decode($json_data)->code;
        if ($data_code == 0 ){
            return array('success'=>true,'code'=>200);
        }else{
            return array('success'=>false,'code'=>8010);
        }
    }
}